﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Taype.Trading.BTC.MtGoxAPIClient;
using Taype.Trading.BTC.MtGoxAPIClient.JSONObjects;
using log4net;

namespace Taype.Trading.BTC.UI.Samples
{
    public partial class MtGoxTickerDemo : Form
    {

        #region "Instance Variables"

        System.Threading.Timer tickerUIUpdateTimer;
        private readonly ILog logger;
        v0 _client;
        MtGoxTickerItem _currentTick;
        List<MtGoxTickerItem> _tickItems = new List<MtGoxTickerItem>();

        #endregion

        public MtGoxTickerDemo()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();
            logger = LogManager.GetLogger(typeof(MtGoxTickerDemo));
            _client = new v0();
            tickerUIUpdateTimer = new System.Threading.Timer(new TimerCallback(UpdateTickerUI), null, 1000, 10000);
        }

        private void UpdateTickerUI(object state)
        {
            ticker_backgroundWorker.RunWorkerAsync();
            this.BeginInvoke(new MethodInvoker(UpdateTickerUI));
        }

        private void UpdateTickerUI()
        {
            if (_currentTick != null)
            {
                // update the ticker info
                //string outStr = string.Format("LAST: {3}   HIGH: {2}   AVG: {0}   BUY: {1}   LAST_ALL: {4}   LAST_LOCAL: {5}   LOW: {6}   SELL: {7}   VOL: {8}   VWAP: {9}", new object[] { _currentTick.avg, _currentTick.buy, _currentTick.high, _currentTick.last, _currentTick.last_all, _currentTick.last_local, _currentTick.low, _currentTick.sell, _currentTick.vol, _currentTick.vwap });
                tickerAvg_textBox.Text = _currentTick.avg.ToString();
                tickerBuy_textBox.Text = _currentTick.buy.ToString();
                tickerHigh_textBox.Text = _currentTick.high.ToString();
                tickerLast_textBox.Text = _currentTick.last.ToString();
                tickerLastAll_textBox.Text = _currentTick.last_all.ToString();
                tickerLastLocal_textBox.Text = _currentTick.last_local.ToString();
                tickerLow_textBox.Text = _currentTick.low.ToString();
                tickerSell_textBox.Text = _currentTick.sell.ToString();
                tickerVol_textBox.Text = _currentTick.vol.ToString();
                tickerVwap_textBox.Text = _currentTick.vwap.ToString();
                tickerLastUpdate_textBox.Text = DateTime.UtcNow.ToLongTimeString();
            }
        }

        /// <summary>
        /// Background worker for the thread that polls MtGox ticker via HTTP API
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ticker_backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                // get the currenct ticker
                _currentTick = _client.ticker();
                // you can also keep a list of ticks ...
                //_tickItems.Add(_currentTick);
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("Error occured while querying ticker: {0}", ex.Message);
            }
        }

    }
}
